package o.mrs;

import java.io.UnsupportedEncodingException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Encounter extends Activity {

	private ListView lista;
	private Button boton;
	private TextView tv;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.encounter);
        
        Bundle bolsa = getIntent().getExtras();
        final String uri = bolsa.getString("URI");
        String uriFull ="encounter?patient="+uri;
        String[] displayEncounter; 
        String obj;
		try {
			//Obtiene pacientes buscados
			obj = ApiRest.getRequestGet(uriFull);
			JSONObject jsonObject = new JSONObject(obj);
	     	JSONArray arrayResult = (JSONArray) jsonObject.get("results");    
	     	displayEncounter = new  String[arrayResult.length()];
	     		     	
	     	for(int contador=0; contador < arrayResult.length(); contador ++){
	     		JSONObject registro = (JSONObject) arrayResult.get(contador);	     				
	     		String display = (String) registro.get("display");  	     		
	     		displayEncounter[contador] = display ; 	     			
	     	}
	     	//lista pacientes encontrados
	     	lista=(ListView) findViewById(R.id.enconunterlist); 
	         ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, displayEncounter); 
	         lista.setAdapter(adapter);
	     		
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		 
		  boton=(Button) findViewById(R.id.enconunterButton);		  
		  boton.setOnClickListener(new OnClickListener() {
			  
			//AGREGAR ENCUENTROS			  
			public void onClick(View v) {
				 final String[] nombresEnc={"ADULTINITIAL","ADULTRETURN","PEDSINITIAL","PEDSRETURN"};
			
				 
				 
				  	AlertDialog.Builder dialogoMultiple= new AlertDialog.Builder(Encounter.this);					
					//dialogoMultiple.setTitle(R.string.titulo);			
					dialogoMultiple.setItems(nombresEnc, new DialogInterface.OnClickListener() {
							
						public void onClick(DialogInterface dialog, int item) {
							Calendar calendario = GregorianCalendar.getInstance();
						    Date fecha = calendario.getTime();
						    Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						    String dateTime = formatter.format(fecha).toString();
						    EncounterType enc = new EncounterType();
						    String[] type=enc.encounter();

							try {
								StringEntity json = new StringEntity( "{\"encounterDatetime\": \"" + "2010-08-12 06:50:04" + "\", " + 
													"\"patient\" : \"" + uri + "\", " + 
													"\"encounterType\": \"" +type[item]+ "\"" + 
													"}");
								
								json.setContentType("application/json");					    
							    ApiRest.getRequestPost("encounter", json);
							    
							} catch (UnsupportedEncodingException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						    
						    
						    
						   
						}
					});
					
					dialogoMultiple.show();
				
			}
		});
		 
		  
        
	}
	

	
}
